﻿var SrcollWidth = 18;       //滚动条宽度
var MarginLeftRight = 4;    //控件左右间距
var MarginTopBottom = 2;

$(function() {
    //初始化布局
    if (typeof (InitLayout) == "function") {
        InitLayout();
    }
});

var timer = null;
window.onresize = function() {
    if (timer == null) {
        timer = setTimeout(function() {
            InitLayout();
            timer = null;
        }, 100);
    }
}

function InitLayout() {

    $(".scrollwidth").width(SrcollWidth);

    var bodyObj = $("body");
    var isExist = existCalcValue(bodyObj);
    if (isExist == true) {
        CalcChildElement(bodyObj);
    }

}

//可以被外界引用
function CalcLayout(parentObj) {
	
    //第一部分：轮廓布局
    parentObj.find(".calcwidth,.calcheight").each(function(index, obj) {
        var currentObj = $(obj);

        if (currentObj.hasClass("calcwidth")) {
            var width = calcValue(currentObj, "width");
            var borderRightWidth = parseInt(currentObj.css("border-right-width"));
            var borderLeftWidth = parseInt(currentObj.css("border-left-width"));
            var marginRight = parseInt(currentObj.css("margin-right"));
            var marginLeft = parseInt(currentObj.css("margin-left"));
            if (isNaN(borderRightWidth)) { borderRightWidth = 0; }
            if (isNaN(borderLeftWidth)) { borderLeftWidth = 0; }
            if (isNaN(marginRight)) { marginRight = 0; }
            if (isNaN(marginLeft)) { marginLeft = 0; }

            width = width - borderRightWidth - borderLeftWidth - marginRight - marginLeft;

            if (currentObj.is("input") || currentObj.is("textarea") || currentObj.is("select")) {
                if (parentObj.children().length <= 1) {
                    width = width - MarginLeftRight * 2;
                }
                else if (parentObj.children().length > 1) {
                    width = width - MarginLeftRight;
                }
            }

            currentObj.width(width);
        }
        
        if (currentObj.hasClass("calcheight")) {
            var height = calcValue(currentObj, "height");
            currentObj.height(height);
        }

    });

    //处理fullwidth和fullheight
    parentObj.find(".fullwidth,.fullheight").each(function(index, obj) {
        var currentObj = $(obj);

        if (currentObj.hasClass("fullwidth")) {
            var width = calcValue(currentObj, "width");
            var borderRightWidth = parseInt(currentObj.css("border-right-width"));
            var borderLeftWidth = parseInt(currentObj.css("border-left-width"));
            if (isNaN(borderRightWidth)) { borderRightWidth = 0; }
            if (isNaN(borderLeftWidth)) { borderLeftWidth = 0; }

            width = width - borderRightWidth - borderLeftWidth;
            
            //有滚动条
            if(currentObj.parent().hasClass("onlyscrolly"))
            {
            	width=width-SrcollWidth;
            }

            currentObj.width(width);
        }
    });
}

function CalcForm(parentObj) {
    //第二部分：处理控件
    var formObj = $(".formcontainer");
    if (formObj.length > 0) {
        formObj.find("input,textarea,select,font,span").each(function(index, obj) {
            var currentObj = $(obj);
            ProcessFormControl(currentObj);
        });
    }
}

function CalcChildElement(parentObj) {

    CalcLayout(parentObj);

    CalcForm(parentObj);
}

function calcValue(oneObj, type) {

    var returnValue = 0;
    var otherValue = 0;
    var parentObj = oneObj.parent();
    if (type == "width") {
        var parentWidth = parentObj.width();
        var isBreak=false;
        parentObj.children().each(function(index, obj) {
            var currentObj = $(obj);

            if(currentObj.hasClass("clrb"))
            {
            	//遇到clrb换行
            	isBreak=true;
            }

            if (currentObj.css("display") == "none" && currentObj.hasClass("hiddencalc") == false) {

            }
            else {
                if (currentObj.hasClass("calcwidth")) {
                    //什么也不做
                }
                else if (currentObj.hasClass("fullwidth")) {
                    //默认一个集合下的fullwidth一样width
                	var borderRightWidth = parseInt(currentObj.css("border-right-width"));
                    var borderLeftWidth = parseInt(currentObj.css("border-left-width"));
                    var marginRight = parseInt(currentObj.css("margin-right"));
                    var marginLeft = parseInt(currentObj.css("margin-left"));
                    if (isNaN(borderRightWidth)) { borderRightWidth = 0; }
                    if (isNaN(borderLeftWidth)) { borderLeftWidth = 0; }
                    if (isNaN(marginRight)) { marginRight = 0; }
                    if (isNaN(marginLeft)) { marginLeft = 0; }
                    
                    //otherValue = borderRightWidth + borderLeftWidth + marginRight + marginLeft;
                    otherValue =  marginRight + marginLeft;
                	returnValue=parentWidth - otherValue;
                	
                	return returnValue;
                }
                else {
                	if(!isBreak)
                	{
	                    var borderRightWidth = parseInt(currentObj.css("border-right-width"));
	                    var borderLeftWidth = parseInt(currentObj.css("border-left-width"));
	                    var marginRight = parseInt(currentObj.css("margin-right"));
	                    var marginLeft = parseInt(currentObj.css("margin-left"));
	                    if (isNaN(borderRightWidth)) { borderRightWidth = 0; }
	                    if (isNaN(borderLeftWidth)) { borderLeftWidth = 0; }
	                    if (isNaN(marginRight)) { marginRight = 0; }
	                    if (isNaN(marginLeft)) { marginLeft = 0; }
	
	                    if (currentObj.is("input") || currentObj.is("textarea") || currentObj.is("select") || currentObj.is("font") || currentObj.is("span")) {
	                        var childrenCount = parentObj.children().length;
	                        otherValue = otherValue + MarginLeftRight * childrenCount;
	                    }
	
	                    otherValue = otherValue + currentObj.width() + borderRightWidth + borderLeftWidth + marginRight + marginLeft;
                	}
                }
            }
            
            
            
            
        });
        returnValue = parentWidth - otherValue;
        if (returnValue <= 0) {
            returnValue = oneObj.width();
        }
    }
    else if (type == "height") {
        var parentHeight = parentObj.height();
        parentObj.children().each(function(index, obj) {
            var currentObj = $(obj);
            if (currentObj.css("display") == "none" && currentObj.hasClass("hiddencalc") == false) {

            }
            else {
                if (currentObj.hasClass("calcheight") || currentObj.hasClass("clearboth")) {
                    //什么也不做
                }
                else {
                    otherValue = otherValue + currentObj.height();
                }
            }
        });
        returnValue = parentHeight - otherValue;
        if (returnValue <= 0) {
            returnValue = oneObj.height();
        }
    }

    return returnValue;
}

function existCalcValue(parentObj) {
    var isExist = false;

    parentObj.find(".calcwidth,.calcheight").each(function(index, obj) {
        isExist = true;

        //return isExist;
        return false; //break

    });

    return isExist;
}

function ProcessFormControl(currentElement) {

    var parentElement = currentElement.parent();

    var parentWidth = parentElement.width();
    var parentHeight = parentElement.height();
    var width = currentElement.width();
    var height = currentElement.height();
    if (width == 0 && height == 0) {
        return true;
    }

    var type = "";
    if (currentElement.is("input")) {
        type = currentElement.attr("type");
    }

    var borderTopWidth = parseInt(currentElement.css("border-top-width"));
    var borderRightWidth = parseInt(currentElement.css("border-right-width"));
    var borderBottomWidth = parseInt(currentElement.css("border-bottom-width"));
    var borderLeftWidth = parseInt(currentElement.css("border-left-width"));
    if (isNaN(borderTopWidth)) { borderTopWidth = 0; }
    if (isNaN(borderRightWidth)) { borderRightWidth = 0; }
    if (isNaN(borderBottomWidth)) { borderBottomWidth = 0; }
    if (isNaN(borderLeftWidth)) { borderLeftWidth = 0; }

    if (currentElement.is("input")) {
        if (type == "text") {

            var newHeight = parentHeight - borderTopWidth - borderBottomWidth - MarginTopBottom * 2;

            currentElement.height(newHeight);
            currentElement.css("margin-left", MarginLeftRight + "px");
            currentElement.css("margin-top", MarginTopBottom + "px");
            currentElement.css("line-height", newHeight + "px");

        }
        else if (type == "button") {

            var newHeight = parentHeight - MarginTopBottom * 2;

            currentElement.height(newHeight);
            currentElement.css("margin-left", MarginLeftRight + "px");
            currentElement.css("margin-top", MarginTopBottom + "px");

            newHeight = newHeight - borderTopWidth - borderBottomWidth;
            currentElement.css("line-height", newHeight + "px");

        }
        else if (type == "radio" || type == "checkbox") {

            currentElement.css("margin-left", MarginLeftRight + "px");
            var newMarginTopBottom = parseInt((parentHeight - height - borderTopWidth - borderBottomWidth) / 2);

            //Math.floor
            //Math.ceil

            if (newMarginTopBottom >= 0) {
                currentElement.css("margin-top", newMarginTopBottom + "px");
            }

        }
        else if (type == "file") {

            var newHeight = parentHeight - MarginTopBottom * 2;

            currentElement.height(newHeight);
            currentElement.css("margin-left", MarginLeftRight + "px");
            currentElement.css("margin-top", MarginTopBottom + "px");
            currentElement.css("line-height", newHeight + "px");
        }
    }
    else if (currentElement.is("textarea")) {

        var newHeight = parentHeight - borderTopWidth - borderBottomWidth - MarginTopBottom * 2;

        currentElement.height(newHeight);
        currentElement.css("margin-left", MarginLeftRight + "px");
        currentElement.css("margin-top", MarginTopBottom + "px");

    }
    else if (currentElement.is("select")) {

        currentElement.css("margin-left", MarginLeftRight + "px");
        var newMarginTopBottom = parseInt((parentHeight - height - borderTopWidth - borderBottomWidth) / 2);
        if (newMarginTopBottom >= 0) {
            currentElement.css("margin-top", newMarginTopBottom + "px");
        }
    }
    else if (currentElement.is("span")) {

        currentElement.css("margin-right", MarginLeftRight + "px");
        var newMarginTopBottom = parseInt((parentHeight - height - borderTopWidth - borderBottomWidth) / 2);
        if (newMarginTopBottom >= 0) {
            currentElement.css("margin-top", newMarginTopBottom + "px");
        }
    }
    else if (currentElement.is("font")) {
        currentElement.css("line-height", parentHeight + "px");

    }
}